package com.ocom.emqx.util;

import java.util.Random;

public class DesUtils {

    public   final static   int[] datakey = new int[] { 0x53, 0x4A, 0x28,
            0x3B, 0x76, 0x7C };

    public  final static  int[] rsnkey = new int[] { 0x3A, 0x25, 0x15,
            0x8F };



    //数据加密
    public static String Cmd_Des(String hexstr, int[] datakey) {
        String hexresult = "";
        for (int i = 0; i < hexstr.length() / 2; i++) {
//按字节截取数据
            String subStr = hexstr.substring(i * 2, i * 2 + 2);
//转 10 进制并跟对应秘钥运算
            int tmp = (hexToInt(subStr) + datakey[i % 6]) % 0x100;
//组合成 16 进制字符串
            hexresult += intToHex(tmp);
        }
        return hexresult;
    }


    //数据解密
    public static String Cmd_UnDes(String hexstr, int[] datakey) {
        String hexresult = "";
        for (int i = 0; i < hexstr.length() / 2; i++) {
//按字节(两个字符)截取数据
            String subStr = hexstr.substring(i * 2, i * 2 + 2);
//转换成十进制 跟对应秘钥参数做运算
            int tmp = (hexToInt(subStr) - datakey[i % 6] + 0x100) % 0x100;
            hexresult += intToHex(tmp);//组合成十六进制字符串
        }
        return hexresult;
    }

    //16 转 10 进制
    public static Integer hexToInt(String hexstr) {
        return Integer.valueOf(hexstr, 16);
    }

    //10 转 16 进制
    public static String intToHex(int para) {
        return String.format("%02x", para).toUpperCase();
    }

    /**
     * 十进制转16进制，并补零
     * @param para 十进制参数
     * @param length 需要的位数长度
     * @return
     */
    public static String intToHexWithFillZero(int para,int length){
        int requiredLength = 2 * length; // 需要两位十六进制数
        return String.format("%0" + requiredLength + "X", para).toUpperCase();
    }


    public static String intToHex(int para, int hexbit) {
        return String.format("%0" + String.valueOf(hexbit) + "x", para);
    }

    //计算异或和(校验码)
    public static String xorStr(String hexstr) {
        int xorcode = 0;
        for (int i = 0; i < hexstr.length() / 2; i++) {
//截取一个字节(两个字符)
            String substr = hexstr.substring(i * 2, i * 2 + 2);
            int tmpint = hexToInt(substr);//十六进制转换成十进制
            xorcode = xorcode ^ tmpint; //异或换算
        }
        return intToHex(xorcode);//格式化输出一个字节 16 进制
    }

    //随机换算
    public static String Cmd_DesRsn(String hexstr, int[] rsnkey) {
        String hexresult = "";
        for (int i = 0; i < hexstr.length() / 2; i++) {
//按字节截取数据
            String subStr = hexstr.substring(i * 2, i * 2 + 2);
//转 10 进制并跟对应秘钥运算
            int tmp = (hexToInt(subStr) + rsnkey[i % 4]) % 0x100;
//组合成 16 进制字符串
            hexresult += intToHex(tmp);
        }
        return hexresult;
    }


    //转 ascii 字符
    //16 进制转 10 进制后在转对应的 ascii 字符
    public static String hexToAscii(String para) {
        int length = para.length() / 2;
        String[] dateArr = new String[length];
        for (int i = 0; i < length; i++) {
            dateArr[i] = para.substring(i * 2, i * 2 + 2);
        }
        StringBuffer sbu = new StringBuffer();
        for (int i = 0; i < dateArr.length; i++) {
            int tmp = hexToInt(dateArr[i]);
            sbu.append((char) tmp);
        }
        return sbu.toString();
    }



    public static void main(String[]  args){
        System.out.println(Cmd_UnDes("34B283B767D534A283B767C534A283B",datakey));

        String encDate = DesUtils.Cmd_Des("20200427163620",DesUtils.datakey);
        System.out.println(encDate);

        System.out.println( DesUtils.xorStr("000720200427163620"));


//        String objectMap =  EmqxApiUtil.doGet("http://47.113.206.142:18083/api/v4/routes");
//
//        System.out.println( objectMap);



//        03 0006 005200000001 06

        String encDate2 = DesUtils.Cmd_Des("005200000001",DesUtils.datakey);

        System.out.println( encDate2);
        System.out.println( DesUtils.xorStr("0006005200000001"));


        for (int i =0;i<100;i++){
            // 创建一个Random类的实例
            Random random = new Random();
            // 生成一个随机整数
            System.out.println( random.nextInt(3000));
        }


        //E1685B7CF159E1585B7CF149E1585B
    }
}
